#include	"RssContent.hpp"
#include	"XmlParser.hpp"
#include	<iostream>
#include	<fstream>
//#include	<windows.h>
#include	"RssItem.hpp"
#include	<string.h>

char *myStrToLower(char *str)
{
	int				i = 0;

	while (str[i])
	{
		if (str[i] >= 'A' && str[i] <= 'Z')
			str[i] += 'a' - 'A';
		++i;
	}
	return (str);
}

using namespace std;
RssContent::RssContent()
{
	_xmlVersion = "";
	_rssVersion = "";
	_rssTitle = "";
	_rssLink = "";
	_rssDescription = "";
	_rssLanguage = "";
	_rssCopyright = "";
	_rssPubDate = "";
	_articleList.clear();
}
RssContent::RssContent(const std::string &file)
{
	setContent(file);
	//Utiliser la classe qui parse le xml
}
RssContent::~RssContent()
{
}
/*
-- Method
*/
void						RssContent::setContent(const std::string &file)
{
	XmlParser				parser;
	vector<RssItem*>	myArticle;
	int						length = 0;
	int						item = 0;

	myArticle.clear();


	parser.setText(file);
	parser.consumeHeaderHttp();
	string	tmp;
	
	tmp = parser.findNextTag();

	while (tmp != "/rss" && tmp != "")
	{
		// cout << "tmp: " << tmp << " --> ";
		if (strstr(myStrToLower((char *)tmp.c_str()), "?xml") != NULL)
		{
			// cout << "XML VERSION" << endl;
			tmp = parser.consumeNextTag();
			this->setXmlVersion(tmp);
		}
		else if (strstr(myStrToLower((char *)tmp.c_str()), "rss") != NULL)
		{
			// cout << "RSS VERSION" << endl;
			tmp = parser.consumeNextTag();
			this->setRssVersion(tmp);
		}
		else if (strstr(myStrToLower((char *)tmp.c_str()), "channel") != NULL)
		{
			// cout << "CHANNEL" << endl;
			tmp = parser.consumeNextTag();
		}
		else if (strstr(myStrToLower((char *)tmp.c_str()), "title") != NULL)
		{
			// cout << "TITLE" << endl;
			tmp = parser.getCompleteLine();
			this->setRssTitle(parser.getBody(tmp));
		}
		else if (strstr(myStrToLower((char *)tmp.c_str()), "link") != NULL)
		{
			// cout << "LINK" << endl;
			tmp = parser.getCompleteLine();
			this->setRssLink(parser.getBody(tmp));
		}
		else if (strstr(myStrToLower((char *)tmp.c_str()), "description") != NULL)
		{
			// cout << "DESCRPTION" << endl;
			tmp = parser.getCompleteLine();
			this->setRssDescription(parser.getBody(tmp));
		}
		else if (strstr(myStrToLower((char *)tmp.c_str()), "language") != NULL)
		{
			// cout << "LANGUAGE" << endl;
			tmp = parser.getCompleteLine();
			this->setRssLanguage(parser.getBody(tmp));
		}
		else if (strstr(myStrToLower((char *)tmp.c_str()), "copyright") != NULL)
		{
			// cout << "COPYRIGHT" << endl;
			tmp = parser.getCompleteLine();
			this->setRssCopyright(parser.getBody(tmp));
		}
		else if (strstr(myStrToLower((char *)tmp.c_str()), "pubdate") != NULL)
		{
			// cout << "PUBDATE" << endl;
			tmp = parser.getCompleteLine();
			this->setRssPubDate(parser.getBody(tmp));
		}
		else if (strstr(myStrToLower((char *)tmp.c_str()), "item") != NULL)
		{
			RssItem		*currItem;
			
			currItem = new RssItem;
			// cout << "ITEM" << endl;
			tmp = parser.consumeNextTag();
			tmp = parser.findNextTag();
			while (strstr(myStrToLower((char *)tmp.c_str()), "/item") == NULL)
			{
				// cout << "Item --> " << tmp << " --> "; 
				if (strstr(myStrToLower((char *)tmp.c_str()), "title") != NULL)
				{
					// cout << "TITLE" << endl;
					tmp = parser.getCompleteLine();
					currItem->setTitle(parser.getBody(tmp));
				}
				else if (strstr(myStrToLower((char *)tmp.c_str()), "description") != NULL)
				{
					// cout << "DESCRIPTION" << endl;
					tmp = parser.getCompleteLine();
					currItem->setDescription(parser.getBody(tmp));
				}
				else if (strstr(myStrToLower((char *)tmp.c_str()), "guid") != NULL)
				{
					// cout << "GUID" << endl;
					tmp = parser.getCompleteLine();
					currItem->setGuid(parser.getBody(tmp));
				}
				else if (strstr(myStrToLower((char *)tmp.c_str()), "link") != NULL)
				{
					// cout << "LINK" << endl;
					tmp = parser.getCompleteLine();
					currItem->setLink(parser.getBody(tmp));
				}
				else if (strstr(myStrToLower((char *)tmp.c_str()), "autor") != NULL)
				{
					// cout << "AUTOR" << endl;
					tmp = parser.getCompleteLine();
					currItem->setAutor(parser.getBody(tmp));
				}
				else if (strstr(myStrToLower((char *)tmp.c_str()), "categorie") != NULL)
				{
					// cout << "CATEGORIE" << endl;
					tmp = parser.getCompleteLine();
					currItem->setCategorie(parser.getBody(tmp));
				}
				else if (strstr(myStrToLower((char *)tmp.c_str()), "pubdate") != NULL)
				{
					// cout << "PUBDATE" << endl;
					tmp = parser.getCompleteLine();
					currItem->setPubDate(parser.getBody(tmp));
				}
				else if (tmp[tmp.size() - 1] == '/')
				{
					// cout << "NOT FOUND MONO" << endl;
					tmp = parser.consumeNextTag();
				}
				else
				{
					// cout << "NOT FOUND" << endl;
					tmp = parser.getCompleteLine();
				}
				tmp = parser.findNextTag();
			}
			tmp = parser.consumeNextTag();
			myArticle.push_back(currItem);
		}
		else if (tmp[tmp.size() - 1] == '/')
		{
			// cout << "NOT FOUND MONO" << endl;
			tmp = parser.consumeNextTag();
		}
		else
		{
			// cout << "NOT FOUND" << endl;
			tmp = parser.getCompleteLine();
		}
		tmp = parser.findNextTag();
	}
	this->setArticleList(myArticle);
	return ;
}
/*
-- Accessor / Mutator
*/
const std::string &			RssContent::getXmlVersion() const
{
	return (this->_xmlVersion);
}

const std::string &			RssContent::getRssVersion() const
{
	return (this->_rssVersion);
}
const std::string &			RssContent::getRssTitle() const
{
	return (this->_rssTitle);
}
const std::string &			RssContent::getRssLink() const
{
	return (this->_rssLink);
}
const std::string &			RssContent::getRssDescription() const
{
	return (this->_rssDescription);
}
const std::string &			RssContent::getRssLanguage() const
{
	return (this->_rssLanguage);
}
const std::string &			RssContent::getRssCopyright() const
{
	return (this->_rssCopyright);
}
const std::string &			RssContent::getRssPubDate() const
{
	return (this->_rssPubDate);
}
const std::vector<RssItem*> &	RssContent::getArticleList() const
{
	return (this->_articleList);
}

void						RssContent::setXmlVersion(const std::string &val)
{
	this->_xmlVersion = val;
	return ;
}

void						RssContent::setRssVersion(const std::string &val)
{
	this->_rssVersion = val;
	return ;
}

void						RssContent::setRssTitle(const std::string &val)
{
	this->_rssTitle = val;
	return ;
}

void						RssContent::setRssLink(const std::string &val)
{
	this->_rssLink = val;
	return ;
}

void						RssContent::setRssDescription(const std::string &val)
{
	this->_rssDescription = val;
	return ;
}

void						RssContent::setRssLanguage(const std::string &val)
{
	this->_rssLanguage = val;
	return ;
}

void						RssContent::setRssCopyright(const std::string &val)
{
	this->_rssCopyright = val;
	return ;
}

void						RssContent::setRssPubDate(const std::string &val)
{
	this->_rssPubDate = val;
	return ;
}

void						RssContent::setArticleList(const std::vector<RssItem*> &val)
{
	this->_articleList = val;
	return ;
}
